//
//  AppDelegate.swift
//  ZBWb
//
//  Created by 天隐翎 on 16/4/6.
//  Copyright © 2016年 csdn. All rights reserved.

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        let account = ZBuserInfoModelViewOuath.shareInfoModelViewOuath.loadInfo()
        print(account)
        
//       注册通知
        registerNotification()
        
        
        // Override point for customization after application launch.
        window = UIWindow(frame: (UIScreen.mainScreen().bounds))
        
        window?.backgroundColor = UIColor.whiteColor()
        
//        window?.rootViewController = MainTabBarViewController()
        window?.rootViewController = userController()
        
        window?.makeKeyAndVisible()
        
        return true
    }
    
//    根据用户登录,来选择相应的控制器
    private func userController()->UIViewController{
        return ZBuserInfoModelViewOuath.shareInfoModelViewOuath.userLogin ? WelcomeViewController():MainTabBarViewController()
        
    }
  
    //    注册通知
    private func registerNotification(){
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeRootViewController:", name: ZBSwitchRootViewController, object: nil)
        
    }
    
//    更改控制器
    @objc private func changeRootViewController(n:NSNotification){
        
        print(n)
//        切换根控制器
        if n.object != nil {
            window?.rootViewController = MainTabBarViewController()
        }else{
//            从授权页面发出的通知
            window?.rootViewController = WelcomeViewController()
            
        }
 
    }
    
//    删除通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    


}

